import { RESPONSE_CODE, STORE_KEY } from "@/common/config";
import { handleInitStoreBaseInfo, handleLoginCallback, showToast } from "@/common/util";
import PendingPop from "@/components/pendingPop/pendingPop";

import useProfileInfoAction from "@/hooks/useProfileInfoAction";
import { IUserProfileEditPromise, USER_PROFILE_STATUS } from "@/typing";

import { View, Input } from "@tarojs/components";
import Taro, { useDidShow } from "@tarojs/taro";
import { Suspense, useState } from "react";


const EditNickName: React.FC = () => {
    const [pendingPopShow, setPendingPopShow] = useState(false)


    const [nickname, setNickname] = useState('');
    const { handleShopUserprofileEdit } = useProfileInfoAction()
    const handleNicknameInput = (e) => {
        const name = e.target.value
        setNickname(name)


    }
    const handdleInitNickName = () => {
        // 初始化昵称
        const baseUserInfo = handleInitStoreBaseInfo()
        if (baseUserInfo) {

            const name = baseUserInfo['nickname']
            console.log(name)
            Taro.setStorageSync(STORE_KEY.EDIT_NICKNAME, name)
            setNickname(name)
        }
    }
    const handleSaveNickName = async () => {
        // const storeNickname = Taro.getStorageSync(STORE_KEY.EDIT_NICKNAME)
        if (nickname) {
            const editRes = await handleShopUserprofileEdit({ nickname: nickname }) as IUserProfileEditPromise
            Taro.hideLoading({

            })
            const { code, msg,data } = editRes
            if (code === RESPONSE_CODE.SUCCESS) {
                const storeBaseUserInfo = handleInitStoreBaseInfo()
                storeBaseUserInfo['modify_field']['nickname'] = USER_PROFILE_STATUS.disable
                Taro.setStorageSync(STORE_KEY.BASE_USER_INFO, JSON.stringify(storeBaseUserInfo))

                showToast(msg)
                if(data.status ===USER_PROFILE_STATUS.pending ){
                    setPendingPopShow(true)
                }else{
                    storeBaseUserInfo['nickname'] = nickname
                    Taro.setStorageSync(STORE_KEY.BASE_USER_INFO, JSON.stringify(storeBaseUserInfo))
                    Taro.navigateBack()
                }
               



            } else if (code === RESPONSE_CODE.LOGIN_EXPIRE || code === RESPONSE_CODE.LOGIN_NEED) {

                handleLoginCallback(() => handleSaveNickName())

            } else {
                showToast(msg)
            }
        } else {
            showToast('昵称不能为空')


        }

    }
    const handleCallBack = () => {
        setPendingPopShow(false)
        Taro.navigateBack()

    }
    useDidShow(() => {
        handdleInitNickName()
    })
    return <>
        <View className="bg-#F6F6F6 min-h-100vh w-full pt-84px px-38px box-border">
            <View className="bg-#fff w-full h-108px px-34px box-border">
                <Input className="w-full h-full text-#333333 text-32px" focus={true} value={nickname} onInput={handleNicknameInput}></Input>
            </View>
            <View className="w-464px h-92px flex mx-auto mt-68px items-center justify-center bg-#8232E8 text-#fff font-medium text-30px" onClick={handleSaveNickName}>保存</View>
        </View>
        <Suspense>
            <PendingPop show={pendingPopShow} disableText='昵称' callBack={handleCallBack}></PendingPop>
        </Suspense>


    </>
}
export default EditNickName;